package code.hkgolden.android.activity;

import java.io.IOException;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import code.hkgolden.android.R;
import code.hkgolden.android.core.HkgoldenActivityBase;
import code.hkgolden.android.core.HkgoldenContext;
import code.hkgolden.android.core.HkgoldenTaskManager;
import code.hkgolden.android.data.Channel;
import code.hkgolden.android.service.ContentService;
import code.hkgolden.android.view.NavBarViewHolder;

import com.google.inject.Inject;

public class SelectActivity extends HkgoldenActivityBase {

	int maxPage;

	WebView webView;

	private class SelectChannelHandler extends Handler {
		@Override
		public void handleMessage(Message msg) {
			logger.debug("handleMessage()");
			progressHandler.notify(HkgoldenTaskManager.REQUEST);
			try {
				SelectActivity.super.hkgoldenViewManager.renderSelectTopic(
						webView, SelectActivity.this);
			} catch (IOException e) {
				logger.error("uncaughtException", e);
			}
			progressHandler.notify(HkgoldenTaskManager.LOAD);
			webView.setWebViewClient(new WebViewClient() {
				@Override
				public boolean shouldOverrideUrlLoading(WebView view, String url) {
					logger.trace("url = {}", url);
					if (url.contains("?ch=")) {
						Channel channel = new Channel();
						channel.setKey(url.replace("?ch=", ""));
						logger.trace("channel.getKey() = {}", channel.getKey());
						String chName = null;
						try {
							channel.setValue(contentService.channels(
									SelectActivity.this).get(channel.getKey()));
						} catch (IOException e) {
							logger.trace("channel.getValue() = {}", channel
									.getValue());
						}
						SelectActivity.super.hkgoldenContext.requestTopics(
								channel, SelectActivity.this);
						return true;
					}
					return super.shouldOverrideUrlLoading(view, url);
				}
			});
			progressHandler.notify(HkgoldenTaskManager.STOP);
		}
	}

	private class SelectPageHandler extends Handler {
		@Override
		public void handleMessage(Message msg) {
			logger.debug("handleMessage()");
			progressHandler.notify(HkgoldenTaskManager.REQUEST);
			try {
				SelectActivity.super.hkgoldenViewManager.renderSelectPage(
						maxPage, webView, SelectActivity.this);
			} catch (IOException e) {
				logger.error("uncaughtException", e);
			}
			progressHandler.notify(HkgoldenTaskManager.LOAD);
			webView.setWebViewClient(new WebViewClient() {
				@Override
				public boolean shouldOverrideUrlLoading(WebView view, String url) {
					logger.trace("url = {}", url);
					if (url.contains("?p=")) {
						Integer p = Integer.parseInt(url.replace("?p=", ""));
						logger.trace("p = {}", p);
						final Intent intent = new Intent();
						intent.putExtra(HkgoldenContext.KEY_PAGE, p);
						SelectActivity.this.setResult(Activity.RESULT_OK,
								intent);
						SelectActivity.this.finish();
						return true;
					}
					return super.shouldOverrideUrlLoading(view, url);
				}
			});
			progressHandler.notify(HkgoldenTaskManager.STOP);
		}
	}

	SelectChannelHandler selectChannelHandler = new SelectChannelHandler();

	SelectPageHandler selectPageHandler = new SelectPageHandler();

	HkgoldenTaskManager.ProgressHandler progressHandler;

	@Inject
	ContentService contentService;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		logger.debug("onCreate()");
		super.onCreate(savedInstanceState);

		final NavBarViewHolder navBarViewHolder = super.hkgoldenViewManager
				.createNavBarViewHolder(this, "");
		progressHandler = super.hkgoldenViewManager.createProgressHandler(this);

		maxPage = super.hkgoldenContext.getRequestMaxPage(this);
		webView = (WebView) super.findViewById(R.id.body);
		logger.debug("webView ={}", webView);
		if (maxPage == 0) {
			selectChannelHandler.sendEmptyMessage(0);
		} else {
			selectPageHandler.sendEmptyMessage(0);
		}
	}

}
